package ajaxjs.service;

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.annotation.WebFilter;
import ajaxjs.data.user.Login;

@WebFilter (urlPatterns={"/service/*","/admin_service/*"})
public class Filter implements javax.servlet.Filter {  
    protected String encoding = null;  
    protected FilterConfig filterConfig = null;  
    
    public void init(FilterConfig config) throws ServletException {  
        System.out.println("过滤器初就绪!");  
        this.filterConfig = config;  
        this.encoding = filterConfig.getInitParameter("encoding");  
    }
    
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {  
//    	setEncoding(request, response);
//    	setUser(request, response);
    	HttpServletRequest req = (HttpServletRequest) request;  
    	String url = req.getRequestURI(), method = req.getMethod();
    	
    	if(url.indexOf("admin_service") != -1){
    		HttpSession session = req.getSession();
    		if(Login.isLoginedBySession(session)){
    			// 已登录，可以进行任意操作
    		}else{
    			response.getWriter().write("No auth!");// 没有权限 
    			return;
    		}
    	}else if(url.indexOf("service/user/login") != -1){
    		// 要求登录，当然可以
    	}else if(url.indexOf("service") != -1){
    		// 客户端接口只读，不能写
    		boolean isWriteMethod = method.equals("POST") || method.equals("PUT") || method.equals("DELETE");
//    		if(isWriteMethod){
//    			response.getWriter().write("No auth!");// 没有权限 
//    			return;
//    		}
    	}

    	chain.doFilter(request, response);  
    } 
    
//    /**
//     * 设置请求和响应的编码
//     * @param request
//     * @param response
//     * @throws IOException
//     */
//    private void setEncoding(ServletRequest request, ServletResponse response) throws IOException{
//        String encoding = this.encoding;  
//        if (encoding != null) {  
//            request.setCharacterEncoding(encoding);  
//        } else {  
//            request.setCharacterEncoding("UTF-8");  
//            response.setCharacterEncoding("UTF-8"); 
//        }
//    }
//    
//    private void setUser(ServletRequest request, ServletResponse response) throws IOException, ServletException{
//    	HttpServletRequest req = (HttpServletRequest) request;  
//        HttpSession session = req.getSession();  
//        
//        // 如果session不为空，则跳转到其它页面,否则转到登陆页面  
//        if (session.getAttribute("user") != null) {  
//        	session.setAttribute("user", "guest");
//            //chain.doFilter(request, response);  
//        }else{  
//           // request.getRequestDispatcher("login.jsp").forward(request, response);  
//        }
//    }
    public void destroy() {  
        System.out.println("过滤器销毁!");  
        this.encoding = null;  
        this.filterConfig = null;  
    }  
} 